package com.car.util;

import java.io.IOException;
import java.util.HashMap;

import org.json.JSONObject;

import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;
import com.baidu.aip.util.Util;


public class voiceUtil {
	 //设置APPID/AK/SK
    public static final String APP_ID = "14894072";
    public static final String API_KEY = "TGKpHKwOafI3TZA0ogXuMa0I";
    public static final String SECRET_KEY = "GQjLWsbT5ytG1Gn2O9mbLbZs6O86y12I";
    
    public static void voice(String content) {
        // 初始化一个AipSpeech
        AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        HashMap<String, Object> options = new HashMap<String, Object>();
        options.put("spd", "3");
        options.put("pit", "8");
        options.put("per", "4");
   
        // 调用接口
        TtsResponse res = client.synthesis(content, "zh", 1, options);
        byte[] data = res.getData();
        JSONObject res1 = res.getResult();
        if (data != null) {
            try {
            	String mp3 = "outString.mp3";
                Util.writeBytesToFileSystem(data, mp3);
                bofang mps = new bofang("E:\\Y2\\car\\carRental\\car-Web\\"+mp3);
                mps.play();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (res1 != null) {
            System.out.println(res1.toString(2));
        }
        
    }
}
